package com.shd.edanpei.presenter;

import android.text.TextUtils;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.bean.IdentityBean;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.contract.PerfectDataContract;
import com.shd.edanpei.presenter.contract.PersonalContract;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

public class PersonalDataPersenter extends BasePresenterImpl<PersonalContract.PersonalDataIView> implements PersonalContract.PersonalNeedDataIPresonal {

    public PersonalDataPersenter(PersonalContract.PersonalDataIView mView) {
        super(mView);
    }

    private CustomerBean bean = new CustomerBean();

    @Override
    public void getOrderInfo(final int orderID) {
        ApiCreator.getApi().getOrderInfo(orderID)
                .compose(RxTransformer.<CustomerBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<CustomerBean>() {
                    @Override
                    protected void onSuccess(CustomerBean data) {
                        bean.setOrderId(orderID + "");
                        bean = data;
                        mBaseIView.setData(data);
                    }
                });
    }

    @Override
    public void setAmount(final TextView text) {
        DialogUtil.moneyLimitDialog(mBaseIView.getContext(), "订单金额", Constant.Unit.WANYUAN.getUnit(), new DialogUtil.Listener() {

            @Override
            public void done(String data) {
                mBaseIView.setText(text, data + Constant.Unit.WANYUAN.getUnit());
                bean.setApplyAmount(Integer.parseInt(data));
            }
        });
    }

    @Override
    public void setRepayDate(final TextView textView) {
        DialogUtil.listDialog(mBaseIView.getContext(), "订单期限", R.array.repay_date, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(textView, data);
                bean.setApplyTerm(data);
            }
        });
    }

    @Override
    public void holdRatio(String text) {
        bean.setHoldRatio(text);
    }

    @Override
    public void setcompanyName(String text) {
        bean.setCompanyName(text);
    }


    @Override
    public void setHouse(boolean isChected) {
        bean.setHouse(isChected?"1":"0");
        RxBusCode.house=isChected;
        RxBus.get().send(RxBusCode.ishourse, isChected);

    }

    @Override
    public void setInsurance(boolean isChected) {
        bean.setInsurance(isChected?"1":"0");
        RxBusCode.marry=isChected;
        RxBus.get().send(RxBusCode.isinsurance, isChected);
    }

    @Override
    public void setFund(boolean isChected) {
        bean.setFund(isChected?"1":"0");
        RxBusCode.fun=isChected;
        RxBus.get().send(RxBusCode.isfun, isChected);
    }

    @Override
    public void submit(int orderType) {
        Map<String, Object> map;
        if(orderType==4){
            map = checkParams2();
            if (map != null && !map.isEmpty()) {
                mBaseIView.showLoading("");
                ApiCreator.getApi().submitMeCredit(map)
                        .compose(RxTransformer.<IdentityBean>errorHandle())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<IdentityBean>() {
                            @Override
                            protected void onSuccess(IdentityBean data) {
                                bean.setOrderId(data.getOrderId() + "");
                                bean.setApplyInterest(data.getApplyInterest());
                                RxBus.get().send(RxBusCode.NEED_DATA_SUBMIT_SUCCESS, bean);
                                DialogUtil.infoDialog2(mBaseIView.getContext(),
                                        "提示",
                                        "资料提交成功，请前去完成第3步\"完善资料\"，方可发单成功",
                                        "去完善资料",
                                        new DialogUtil.SureListener() {
                                            @Override
                                            public void sure() {
                                                mBaseIView.getContext().finish();
                                            }
                                        });
                            }

                            @Override
                            public void onFinish() {
                                mBaseIView.stopLoading();
                            }
                        });
            }
        }else{
            map = checkParams3();
            if (map != null && !map.isEmpty()) {
                mBaseIView.showLoading("");
                ApiCreator.getApi().submitTaxOrder(map)
                        .compose(RxTransformer.<IdentityBean>errorHandle())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<IdentityBean>() {
                            @Override
                            protected void onSuccess(IdentityBean data) {
                                bean.setOrderId(data.getOrderId() + "");
                                bean.setApplyInterest(data.getApplyInterest());
                                RxBus.get().send(RxBusCode.NEED_DATA_SUBMIT_SUCCESS, bean);
                                DialogUtil.infoDialog2(mBaseIView.getContext(),
                                        "提示",
                                        "资料提交成功，请前去完成第3步\"完善资料\"，方可发单成功",
                                        "去完善资料",
                                        new DialogUtil.SureListener() {
                                            @Override
                                            public void sure() {
                                                mBaseIView.getContext().finish();
                                            }
                                        });
                            }

                            @Override
                            public void onFinish() {
                                mBaseIView.stopLoading();
                            }
                        });
            }
        }


    }

    @Override
    public void updata(int orderType) {
        Map<String, Object> map;
        if(orderType==4){
          map = checkParams2();
            if (map != null && !map.isEmpty()) {
                mBaseIView.showLoading("正在修改...");
                ApiCreator.getApi().updataMeCredit(map)
                        .compose(RxTransformer.<NoDataResponse>errorHandle())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<NoDataResponse>() {
                            @Override
                            protected void onSuccess(NoDataResponse data) {
                                ToastUtil.showToast("修改成功");
                                RxBus.get().send(RxBusCode.NEED_DATA_SUBMIT_SUCCESS, bean);
                                RxBus.get().send(RxBusCode.other,bean.isHaveOtherHouse());
                                RxBus.get().send(RxBusCode.company,bean.isHaveCompany());
                                mBaseIView.getContext().finish();
                            }

                            @Override
                            public void onFinish() {
                                mBaseIView.stopLoading();
                            }
                        });
            }
        }else{
            map = checkParams3();
            if (map != null && !map.isEmpty()) {
                mBaseIView.showLoading("正在修改...");
                ApiCreator.getApi().updateTaxOrder(map)
                        .compose(RxTransformer.<NoDataResponse>errorHandle())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<NoDataResponse>() {
                            @Override
                            protected void onSuccess(NoDataResponse data) {
                                ToastUtil.showToast("修改成功");
                                RxBus.get().send(RxBusCode.NEED_DATA_SUBMIT_SUCCESS, bean);
                                RxBus.get().send(RxBusCode.other,bean.isHaveOtherHouse());
                                RxBus.get().send(RxBusCode.company,bean.isHaveCompany());
                                mBaseIView.getContext().finish();
                            }

                            @Override
                            public void onFinish() {
                                mBaseIView.stopLoading();
                            }
                        });
            }
        }


    }

    @Override
    public void isMarried(boolean isMarries) {

    }

    @Override
    public void setCustomerBean(CustomerBean bean) {
        if (bean != null) {
            this.bean = bean;
            mBaseIView.setData(bean);
        }
    }

    /**
     * 验证住房所需材料 下
     */
    private Map<String, Object> checkParams2() {
        if("1".equals(bean.getHouse())
                ||"1".equals(bean.getInsurance())
                ||"1".equals(bean.getFund())
                ||"true".equals(bean.getHouse())
                ||"true".equals(bean.getInsurance())
                ||"true".equals(bean.getFund())){

        }else{
            ToastUtil.showToast("请选择贷款产品");
            return  null;
        }
        if (isNull(bean.getApplyAmount(), "请选择订单金额")) return null;
        if (isNull(bean.getApplyTerm(), "请选择订单期限")) return null;

        return GsonUtil.toMap(bean);
    }
    /**
     * 验证住房所需材料 下
     */
    private Map<String, Object> checkParams3() {
        if (isNull(bean.getApplyAmount(), "请选择订单金额")) return null;
        if (isNull(bean.getApplyTerm(), "请选择订单期限")) return null;
        if (isNull(bean.getHoldRatio(), "请输入持股比")) return null;
        if (isNull(bean.getCompanyName(), "请输入企业名称")) return null;

        return GsonUtil.toMap(bean);
    }

    /**
     * 验空
     */
    private boolean isNull(String str, String toast) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.showToast(toast);
            return true;
        }
        return false;
    }
}
